package com.fenghongzhang.lib_jetpack.recyclerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.fenghongzhang.lib_jetpack.R

class MultiTypeAdapter(
    private var itemList: MutableList<Item>,
    private val itemClickListener: (Item) -> Unit,
    private val itemLongClickListener: (Item) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun getItemViewType(position: Int): Int {
        return itemList[position].type
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            Item.TYPE_1 -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.jetpack_item_type1, parent, false)
                Type1ViewHolder(view)
            }
            Item.TYPE_2 -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.jetpack_item_type2, parent, false)
                Type2ViewHolder(view)
            }
            else -> throw IllegalArgumentException("Invalid view type")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        println("onBindViewHolder: $position")
        val item = itemList[position]
        when (holder) {
            is Type1ViewHolder -> {
                holder.bind(item as Item.Type1)
                // 设置点击监听器
                holder.itemView.setOnClickListener {
                    itemClickListener(item)
                }
                holder.itemView.setOnLongClickListener {
                    itemLongClickListener(item)
                    true
                }
            }
            is Type2ViewHolder -> {
                holder.bind(item as Item.Type2)
                holder.itemView.setOnClickListener {
                    itemClickListener(item)
                }
                holder.itemView.setOnLongClickListener {
                    itemLongClickListener(item)
                    true
                }
            }
        }
    }

    override fun onBindViewHolder(
        holder: RecyclerView.ViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        println("onBindViewHolder+payload: $position")
        if (payloads.isEmpty()) {
            // 如果没有传递任何 payload，表示需要进行完整的更新
            super.onBindViewHolder(holder, position, payloads)
        } else {
            // 如果有 payload，表示需要更新局部内容
            // 通常根据 payload 来决定局部更新哪些视图
            for (payload in payloads) {
                // 根据 payload 来做具体的局部更新
                if (payload is String) {
                    // 比如更新 TextView
                    (holder as MultiTypeAdapter.Type1ViewHolder).textView.text = payload
                }
                // 根据不同的 payload 类型做不同的更新
            }
        }
    }


    // 拖动排序处理
    fun onItemMove(fromPosition: Int, toPosition: Int) {
//        val item = items.removeAt(fromPosition)
//        items.add(toPosition, item)
//        notifyItemMoved(fromPosition, toPosition)
    }

    // 删除操作
    fun onItemSwiped(position: Int) {
        itemList.removeAt(position)
        notifyItemRemoved(position)
    }



    override fun getItemCount(): Int {
        return itemList.size
    }

   inner class Type1ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.textViewType1)
        private val deleteButton: Button = itemView.findViewById(R.id.deleteButton1)

        init {
            deleteButton.setOnClickListener {
                itemList.removeAt(position)
                notifyItemRemoved(position)
            }
        }

        fun bind(item: Item.Type1) {
            textView.text = item.text
            deleteButton.setVisibility(View.GONE) // 初始时隐藏删除按钮
        }

        // 更新删除按钮的可见性
        fun showDeleteButton() {
            deleteButton.visibility = View.VISIBLE
        }

        fun hideDeleteButton() {
            deleteButton.visibility = View.GONE
        }
    }

  inner  class Type2ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val textView: TextView = itemView.findViewById(R.id.textViewType2)
        private val deleteButton: Button = itemView.findViewById(R.id.deleteButton2)

        fun bind(item: Item.Type2) {
            textView.text = item.text
            deleteButton.setVisibility(View.GONE) // 初始时隐藏删除按钮
        }

        // 更新删除按钮的可见性
        fun showDeleteButton() {
            deleteButton.visibility = View.VISIBLE
        }

        fun hideDeleteButton() {
            deleteButton.visibility = View.GONE
        }


    }

    // 使用 DiffUtil 更新数据
    fun updateData(newItems: MutableList<Item>) {
        val diffCallback = DiffUtilCallback(itemList, newItems)
        val diffResult = DiffUtil.calculateDiff(diffCallback)
        itemList = newItems
        diffResult.dispatchUpdatesTo(this)
    }

    // DiffUtil.Callback 实现
    class DiffUtilCallback(
        private val oldList: MutableList<Item>,
        private val newList: MutableList<Item>,
    ) : DiffUtil.Callback() {

        override fun getOldListSize(): Int = oldList.size
        override fun getNewListSize(): Int = newList.size

        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            // 判断项是否相同，可以通过 id 或其他字段
            return oldList[oldItemPosition] == newList[newItemPosition]
        }

        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            // 判断项的内容是否相同
            return oldList[oldItemPosition] == newList[newItemPosition]
        }
    }
}

